package com.ourhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ourhealth.constant.MessageConstant;
import com.ourhealth.entity.PageResult;
import com.ourhealth.entity.QueryPageBean;
import com.ourhealth.entity.Result;
import com.ourhealth.pojo.CheckGroup;
import com.ourhealth.service.CheckItemGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkItemGroup")
public class CheckItemGroupController {
    @Reference
    CheckItemGroupService checkItemGroupService;
    @RequestMapping("addCheckItemGroup")
    public Result addCheckItemGroup(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds){
        try {
            checkItemGroupService.addCheckItemGroup(checkGroup,checkItemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemGroupService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("editCheckItemGroup")
    public Result editCheckItemGroup(@RequestBody CheckGroup checkGroup, @RequestParam("checkItemIds") Integer[] checkItemIds){
        try {
            checkItemGroupService.editCheckItemGroup(checkGroup,checkItemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findCheckGroupById")
    public Result findCheckGroupById(Integer id){
        CheckGroup checkGroup = null;
        try {
            checkGroup = checkItemGroupService.findCheckGroupById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @RequestMapping("findChooseCheckItem")
    public Result findChooseCheckItem(Integer id){
        Integer[] ids = null;
        try {
            ids = checkItemGroupService.findChooseCheckItem(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,ids);
    }

    @RequestMapping("deleteCheckGroup")
    public Result deleteCheckGroup(Integer id){
        try {
            checkItemGroupService.deleteCheckGroup(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findAll")
    public Result findAll(){
        List<CheckGroup> list = checkItemGroupService.finAll();

        if (list == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }
}
